package com.biubiushop.socket.bio;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;

import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;

/**
 * @author chenjia
 * @date 2019/7/16
 **/
public class SocketThread implements Runnable {

    Socket socket = null;

    public SocketThread(Socket socket) {
        this.socket = socket;
    }

    private static final Log log = LogFactory.get();

    @Override
    public void run() {
        InputStream bis = null;
        ByteArrayOutputStream baos = null;
        OutputStream os = null;
        OutputStreamWriter osw = null;
        BufferedWriter bw = null;
        try {
            System.out.println("=========线程开始执行============");
            System.out.println("=========线程名称为:" + Thread.currentThread().getName());

            bis = socket.getInputStream();
            baos = new ByteArrayOutputStream();

            String info = null;
            byte[] buffer = new byte[1024];
            int len = 0;

            while(((len = bis.read(buffer)) != -1)){
                baos.write(buffer, 0, len);
                if (len < 1024) {
                    info = baos.toString("gbk");
                    //判断是否为断开指令
                    if(info.trim().equals("exit")){
                        break;
                    }
                    log.info("服务器接收到客户端的信息:" + info);
                    baos = new ByteArrayOutputStream();

                    os = socket.getOutputStream();
                    osw = new OutputStreamWriter(os);
                    bw = new BufferedWriter(osw);
                    bw.write(info);
                    bw.flush();
                    System.out.println("=====服务端响应客户端======");
                }
            }

//            while ((len = bis.read(buffer)) != -1) {
//                baos.write(buffer, 0, len);
//                if (len < 1024) {
//                    info = baos.toString("gbk");
//                    log.info("服务器接收到客户端的信息:" + info);
//                    break;
//                }
//            }
//            socket.shutdownInput();

//            os = socket.getOutputStream();
//            osw = new OutputStreamWriter(os);
//            bw = new BufferedWriter(osw);
//            bw.write(info);
//            bw.flush();
//            socket.shutdownOutput();
            log.info("=====服务端与客户端断开连接======");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bw != null)
                    bw.close();
                if (osw != null)
                    osw.close();
                if (os != null)
                    os.close();

                if (baos != null)
                    baos.close();
                if (bis != null)
                    bis.close();
                if (socket != null)
                    socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
